package cn.wolfcode.web.interceptor;

import org.springframework.stereotype.Component;
import org.springframework.stereotype.Controller;
import org.springframework.web.servlet.HandlerInterceptor;

import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;

@Component
public class LoginInterceptor implements HandlerInterceptor {
    //登陆拦截
    public boolean preHandle(HttpServletRequest request, HttpServletResponse response, Object handler) throws Exception {
        //浏览器--》过滤器--》前端控制器-》拦截器--》控制器
        Object userSession = request.getSession().getAttribute("USER_IN_SESSION");
        if (userSession==null) {
            //没登陆，拦截，不允许放行往下继续走,重定向到登陆页面
            response.sendRedirect("/login.html");
            return false;
        }
        //登陆了，放行，继续往下走
        return true;
    }
}
